/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.martinkrasa.golftourweb.site;

import com.martinkrasa.golftourweb.site.entity.Tournament;
import com.martinkrasa.golftourweb.site.entity.TournamentResult;
import com.martinkrasa.golftourweb.site.form.TournamentLoadForm;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author martin.krasa
 */
@Controller
public class TournamentController {
    
    @Inject TournamentService tournamentService;
    
    @RequestMapping(value = "tournament/{tournamentId}", method = RequestMethod.GET)
    public String viewTournament(Map<String, Object> model, @PathVariable("tournamentId") long id) {
        Tournament tournament = this.tournamentService.getTournament(id);
        List<TournamentResult> results = this.tournamentService.getTournamentResults(id);
        
        model.put("tournament", tournament);
        model.put("results", results);
        
        return "tournament";
    }
    
    @RequestMapping(value = "tournament/load", method = RequestMethod.GET)
    public String loadTournamentForm(Map<String, Object> model) {
        TournamentLoadForm tournamentLoadForm = new TournamentLoadForm();
        tournamentLoadForm.setNetto("420562536");
        tournamentLoadForm.setBrutto("420562538");
        
        model.put("tournamentLoadForm", tournamentLoadForm);
                
        return "admin/loadTournament";
    }
    
    @RequestMapping(value = "tournament/load", method = RequestMethod.POST)
    public View saveTournamentForm(TournamentLoadForm tournamentLoadForm) {
        String[] brutto = new String[1];
        String[] netto = new String[1];
        brutto[0] = tournamentLoadForm.getBrutto();
        netto[0] = tournamentLoadForm.getNetto();
        
        Tournament tournament = this.tournamentService.loadTournament(brutto, netto);
        
        return new RedirectView("/tournament/" + tournament.getId(), true, false);
    }
}
